package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;

import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/16 10:51
 * @Version 1.0
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }
    //删除频道
    @Override
    public ResponseResult delectChannel(Integer id) {
        WmChannel wmChannel=wmChannelMapper.selectById(id);
        if (!wmChannel.getStatus()){
            return ResponseResult.errorResult(500,"频道不可删除");
        }else {
        wmChannelMapper.deleteById(id);}
        return ResponseResult.okResult(id);
    }
    //分页查询
    @Override
    public ResponseResult list(WmChannelDto dto) {
//分页条件查询
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
//判断传入的参数是否为空
        if (StringUtils.isEmpty(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> list = wmChannelMapper.selectPage(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        responseResult.setData(list.getRecords());
        responseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return responseResult;
    }




    //更新频道
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        wmChannelMapper.updateById(wmChannel);
        return null;
    }
    //新增频道
    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName,wmChannel.getName());
        List<WmChannel> wmChannels = wmChannelMapper.selectList(wrapper);
         if (wmChannels.size()>0){
             return ResponseResult.errorResult(500,"频道已存在");
         }
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult(wmChannel);
    }
}